#!/bin/bash

function power_control() {
GPIO_BASE=(`ls /sys/class/gpio`)
i=0
while [ ${GPIO_BASE[${i}]} ]
do
    GPIO_CHIP=${GPIO_BASE[${i}]#*gpiochip}
    GPIO_PATH="/sys/class/gpio/${GPIO_BASE[$i]}/device/gpiochip5"
    if [ ! -e $GPIO_PATH ]
    then
        i=$((i+1))
        continue
    fi
    BASE=$(cat /sys/class/gpio/gpiochip${GPIO_CHIP}/base)
    CONTROL_GPIO=$((${BASE}+2))
    echo $CONTROL_GPIO > /sys/class/gpio/export
    echo high > /sys/class/gpio/gpio${CONTROL_GPIO}/direction
    echo 0 > /sys/class/gpio/gpio${CONTROL_GPIO}/value
    sleep 1
    echo 1 > /sys/class/gpio/gpio${CONTROL_GPIO}/value
    sleep 1
    echo $CONTROL_GPIO > /sys/class/gpio/unexport
    exit 0;
done
}
power_control $1 $2
